package com.qyhl.webtv.commonlib.utils.action;

import android.app.Activity;

import com.alibaba.android.arouter.facade.annotation.Autowired;
import com.alibaba.android.arouter.launcher.ARouter;
import com.orhanobut.hawk.Hawk;
import com.qyhl.webtv.commonlib.BuildConfig;
import com.qyhl.webtv.commonlib.common.CommonUtils;
import com.qyhl.webtv.commonlib.constant.ServicePathConstant;
import com.qyhl.webtv.commonlib.service.UserService;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
 * 用户行为数据工具类
 *
 * @author helin
 * @date 2018/3/21
 */
public class ActionLogUtils {


    @Autowired(name = ServicePathConstant.USER_SERICE)
    UserService userService;
    /**
     * 缓存key
     */
    public static String ACTION_KEY = "ACTION_KEY";
    /**
     * 网络状态
     */
    public static int netWork;

    /**
     * 进入页面的名称
     */
    private String mActName;
    /**
     * 进入页面时间
     */
    private long sTime;

    /**
     * 时间解析
     */
    private SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
    /**
     * 当前界面
     */
    private String mNowScene;

    private ActionLogUtils() {
        //注解初始化
        ARouter.getInstance().inject(this);
    }

    private static ActionLogUtils actionLogUtils = new ActionLogUtils();
    public static String deviceId;

    public static ActionLogUtils getInstance() {
        return actionLogUtils;
    }


    /**
     * 事件
     *
     * @param actionStr
     */
    public void onEvent(String actionStr) {
        ActionLogBean action = getAction();
        action.setAction(actionStr);
        saveData(action);
    }

    public void onEvent(String action, int shopId) {
        ActionLogBean actionBean = getAction();
        actionBean.setAction(action);
        actionBean.setShopId(shopId);
        saveData(actionBean);
    }

    public void onEvent(String action, int spaceId, int advId) {
        ActionLogBean actionBean = getAction();
        actionBean.setAction(action);
        actionBean.setAdvertiseSpaceId(spaceId);
        actionBean.setAdvertisePutId(advId);
        saveData(actionBean);
    }

    /**
     * 页面跳转
     *
     * @param actionStr
     * @param scene     当前页面
     * @param to        跳转页面
     */
    public void onEvent(String actionStr, String scene, String to) {
        ActionLogBean action = getAction();
        action.setAction(actionStr);
        action.setNowScene(scene);
        action.setToScene(to);
        saveData(action);
    }


    /**
     * 分享统计
     *
     * @param title
     * @param type
     * @param id
     */
    public void doShare(String title, String type, String id) {
        ActionLogBean action = getAction();
        action.setAction(ActionConstant.SHARE);
        action.setTitle(title);
        action.setProId(id);
        action.setType(type);
        saveData(action);
    }

    /**
     * onResume
     *
     * @param act 当前activity
     */
    public void onResume(Activity act, String nowScene) {
        sTime = System.currentTimeMillis();
        mNowScene = nowScene;
    }

    /**
     * onPause
     *
     * @param act 当前activity
     */
    public void onPause(Activity act, String nowScene) {
        long time = System.currentTimeMillis();
        if (nowScene.equals(mNowScene) && time - sTime > 1000 && time - sTime < 120 * 1000) {
            ActionLogBean action = getAction();
            action.setNowScene(nowScene);
            action.setAction(ActionConstant.VISITPAGE);
            action.setDuration((int) (time - sTime));
            saveData(action);
        }
    }

    /**
     * 保存用户行为信息
     *
     * @param actionLogBean
     */
    public void saveData(ActionLogBean actionLogBean) {
        List<ActionLogBean> actions;
        if (Hawk.contains(ACTION_KEY)) {
            actions = Hawk.get(ACTION_KEY);
        } else {
            actions = new ArrayList<>();
        }
        actions.add(actionLogBean);
        Hawk.put(ACTION_KEY, actions);
    }

    /**
     * 清楚用户行为信息
     */
    public void clearData() {
        if (Hawk.contains(ACTION_KEY)) {
            Hawk.delete(ACTION_KEY);
        }
    }


    /**
     * 获取用户行为信息
     *
     * @return 上次的用户行为信息
     */
    public List<ActionLogBean> getActionData() {
        if (Hawk.contains(ACTION_KEY)) {
            List<ActionLogBean> data = Hawk.get(ACTION_KEY);
            return data;
        }
        return null;
    }

    public String getUserNmme() {
        return userService.getUserPhone();
    }

    /**
     * 获取 基本的Action
     *
     * @return
     */
    private ActionLogBean getAction() {
        ActionLogBean actionLogBean = new ActionLogBean();
        //行为开始时间
        actionLogBean.setStartTime(simpleDateFormat.format(new Date()));
        //站点
        actionLogBean.setSiteId(CommonUtils.getInstance().getSite());
        //用户电话
        String userName = userService.getUserPhone();
        actionLogBean.setUserName(userName);
        //系统版本
        actionLogBean.setSystemVersion(getSystemVersion());
        //系统厂商
        actionLogBean.setPhoneBrand(getDeviceBrand() + " " + getSystemModel());
        //手机类型
        actionLogBean.setPhoneType("Android");
        //手机唯一Id
        actionLogBean.setDeviceId(deviceId);
        //网络状态 1 无线网络 0  移动网络 -1 没有网络
        if (netWork == 1) {
            actionLogBean.setNetWorkState("WIFI");
        } else if (netWork == 0) {
            actionLogBean.setNetWorkState("4G");
        }
        //版本名称
//        actionLogBean.setAppVersion(BuildConfig.VERSION_NAME);
        return actionLogBean;
    }


    /**
     * 获取当前手机系统版本号
     *
     * @return 系统版本号
     */
    public String getSystemVersion() {
        return android.os.Build.VERSION.RELEASE;
    }

    /**
     * 获取手机型号
     *
     * @return 手机型号
     */
    public String getSystemModel() {
        return android.os.Build.MODEL;
    }

    /**
     * 获取手机厂商
     *
     * @return 手机厂商
     */
    public String getDeviceBrand() {
        return android.os.Build.BRAND;
    }
}
